package com.cmcc.aregister.ui.adapter;

import java.util.List;

import com.cmcc.aregister.R;
import com.cmcc.aregister.cs.asntask.InterfaceUtils.DataCallback_09;
import com.cmcc.aregister.cs.model.AdModel;
import com.cmcc.aregister.cs.model.AdbarResultModel;
import com.cmcc.aregister.cs.util.RemoteImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

public class ImagePagerAdapter extends PagerAdapter {

//	private String[] images;
	private List<AdModel> bannerList;
	private LayoutInflater inflater;
	private DisplayImageOptions options;
	ImageLoader imageLoader;
	DataCallback_09 mDataCallback_09;
	Context context;
	public ImagePagerAdapter(Context context,List<AdModel> bannerList,DisplayImageOptions options,DataCallback_09 mDataCallback_09) {
		this.bannerList = bannerList;
		this.context = context;
		inflater = LayoutInflater.from(context);
		this.options = options;
		imageLoader = ImageLoader.getInstance();
		this.mDataCallback_09 = mDataCallback_09;
	}

	@Override
	public void destroyItem(View container, int position, Object object) {
		((ViewPager) container).removeView((View) object);
	}

	@Override
	public void finishUpdate(View container) {
	}

	@Override
	public int getCount() {
		return Integer.MAX_VALUE;
//		return 3;
	}
	
	OnClickListener mOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View view) {
			// TODO Auto-generated method stub
			int p = (Integer) view.getTag();
			mDataCallback_09.callback(p);
		}
	};

	@Override
	public Object instantiateItem(View view, int position) {
		final FrameLayout imageLayout = (FrameLayout) inflater.inflate(R.layout.item_pager_image, null);
		final ImageView imageView = (ImageView) imageLayout.findViewById(R.id.image);
//		final ImageView spinner = (ImageView) imageLayout.findViewById(R.id.loading);
//		RemoteImageView imageView = new RemoteImageView(context);
		int p = position % bannerList.size();
		//imageView.setImageResource(R.drawable.ad_default_1);
		imageLoader.displayImage(bannerList.get(p).pic, imageView, options, new ImageLoadingListener() {
			@Override
			public void onLoadingStarted() {
//				spinner.setVisibility(View.VISIBLE);
			}

			@Override
			public void onLoadingFailed(FailReason failReason) {
				String message = null;
				switch (failReason) {
					case IO_ERROR:
						message = "Input/Output error";
						break;
					case OUT_OF_MEMORY:
						message = "Out Of Memory error";
						break;
					case UNKNOWN:
						message = "Unknown error";
						break;
				}
//				Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

//				spinner.setVisibility(View.GONE);
//				imageView.setImageResource(android.R.drawable.ic_delete);
			}

			@Override
			public void onLoadingComplete() {
//				spinner.setVisibility(View.GONE);
//				Animation anim = AnimationUtils.loadAnimation(context, R.anim.fade_in);
//				imageView.setAnimation(anim);
//				anim.start();
			}

			@Override
			public void onLoadingCancelled() {
				// Do nothing
			}
		});
//		imageView.setImageUrl(bannerList.get(p).pic);
		((ViewPager) view).addView(imageLayout);
		imageLayout.setOnClickListener(mOnClickListener);
		imageLayout.setTag(p);
		return imageLayout;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view.equals(object);
	}

	@Override
	public void restoreState(Parcelable state, ClassLoader loader) {
	}

	@Override
	public Parcelable saveState() {
		return null;
	}

	@Override
	public void startUpdate(View container) {
	}
}
